package android.moodle;

import android.Graphics.GameRenderer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.javaClasses.Content;
import android.javaClasses.DataHelper;
import android.javaClasses.Game;
import android.javaClasses.Server;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

public class ServerActivity extends Activity {
    private Server server;
    private int numbConnected;
    private ArrayList<String> players = new ArrayList<String>();
    private GameRenderer renderer;
    private GLSurfaceView glView;
    private TextView gameHeader;
    private int typeOfGame;
    private int numPlayers;
    private Context context;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gamescreen);
        context = this;

        Bundle extras = getIntent().getExtras();
        numPlayers = extras.getInt("numPlayers");

        typeOfGame = extras.getInt("typeOfGame");

        final Vibrator v = (Vibrator) getSystemService( Context.VIBRATOR_SERVICE );

        // Get the width and height of the screen, so it's scalable
        Display display = getWindowManager().getDefaultDisplay();
        float width = display.getWidth();

        // Subtracts 25 pixels because that is the height of the statusbar
        float height = display.getHeight() - 25;

        //Declares the GLSurfaceView that is used for drawing the game
        glView = (GLSurfaceView) findViewById(R.id.glsurfaceview);

        //Declares the Gamerenderer that is used to draw the game
        renderer = new GameRenderer( this, width, height, typeOfGame, numPlayers);
        glView.setRenderer( renderer );

        gameHeader = (TextView) findViewById(R.id.gameheader);

        glView.setOnTouchListener( new View.OnTouchListener()
        {
            public boolean onTouch( View view, MotionEvent motionEvent )
            {
                v.vibrate(100);
                float xValue = motionEvent.getX();
                float yValue = motionEvent.getY();
                sendValues(xValue, yValue);
                renderer.drawSymbol(xValue, yValue);
                if(renderer.getPlayerWon()) {
                    String winner = renderer.getWinner();
                    sendFinished(winner);
                    addToDB(winner);

                }
                gameHeader.setText( renderer.getHeader() );
                return false;
            }
        });

        server = new Server(mHandler);
        server.startServer();

    }

    private void sendValues(float xValue, float yValue) {
        server.writeToStream(new Content(Content.SEND_CHOSEN_CELL, xValue, yValue));

    }

    private void sendStartStatus() {
        server.writeToStream(new Content(Content.START_STATUS, typeOfGame, numPlayers));
    }

    private void sendFinished(String winner) {
        server.writeToStream(new Content(Content.GAME_FINISHED, players, winner));
    }

    @Override
    public void onStop() {
        server.stopServer();
    }

    public void addToDB(String winner) {
        Game theGame = new Game(players, winner);
        DataHelper helper = new DataHelper(context);
        helper.insert(theGame);
        startActivity(new Intent(context, ScoresActivity.class));
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Content.SEND_USERNAME:
                    players.add(msg.getData().getString("playerName"));
                    sendStartStatus();
                    break;
                case Content.SEND_CHOSEN_CELL:
                    int xValue = msg.getData().getInt("xValue");
                    int yValue = msg.getData().getInt("yValue");
                    renderer.drawSymbol(xValue, yValue);
                    break;

            }
        }
    };

}
